import java.io.*;
import java.net.*;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
//import java.awt.*;
//import java.awt.event.ActionEvent;
//import java.awt.event.ActionListener;
//import java.beans.EventHandler;
import java.util.*;

import com.sun.nio.sctp.SctpServerChannel;

import com.sun.nio.sctp.AssociationChangeNotification;
import com.sun.nio.sctp.AbstractNotificationHandler;
import com.sun.nio.sctp.HandlerResult;
import com.sun.nio.sctp.MessageInfo;
import com.sun.nio.sctp.SctpChannel;
import com.sun.nio.sctp.ShutdownNotification;
//import Terminal.AssociationHandler;

import static com.sun.nio.sctp.AssociationChangeNotification.AssocChangeEvent.*;


public class ConnectionListener extends Thread {

	//	Terminal hostTerminal;
	//	ServerSocket listeningSocket;

    static int SERVER_PORT = 3456;

	public void run() {
		startListening();
	}

	public ConnectionListener( Terminal host, int port ) throws IOException {
	};

	public void startListening() {

		System.out.println("ConnectionListener - startListening()");


		for ( ;; ) {
			try {
				handleNewConnection();
			} catch (IOException e) {
				System.out.println("ConnectionListener - Error accpeting new connection");
				e.printStackTrace();
			}
		}

	}

	//	private void handleNewConnection () throws IOException {
	//		serverAddr = new InetSocketAddress("localhost", SERVER_PORT);
	//		sc = SctpChannel.open(serverAddr, 0, 0);
	//		printInfo(sc);
	//
	//		do {
	//			messageInfo = sc.receive(buf, System.out, assocHandler);
	//			buf.flip();
	//
	//			if (buf.remaining() > 0 ) {
	//				msgIn=decoder.decode(buf).toString();
	//				System.out.println("MSG REVEIVED: " +msgIn );
	//			}
	//			buf.clear();
	//		} while (messageInfo != null);
	//		sc.close();
	//
	//	}

	private void handleNewConnection () throws IOException {
		SctpServerChannel ssc = SctpServerChannel.open();
		InetSocketAddress serverAddr = new InetSocketAddress(SERVER_PORT);
		ssc.bind(serverAddr);

		ByteBuffer buf = ByteBuffer.allocateDirect(60);
		CharBuffer cbuf = CharBuffer.allocate(60);
		Charset charset = Charset.forName("ISO-8859-1");
		CharsetEncoder encoder = charset.newEncoder();
		BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
		String in;
		while (true) {
			SctpChannel sc;

			if((sc = ssc.accept())!=null)
			{
				printInfo(sc);
			}
			else
			{
				System.out.println("Something is up when trying to connect!\n");
			}

			/* get the current date
        Date today = new Date(); */

			char[] today=new char[1024];

			in= stdIn.readLine();
			today=in.toCharArray();
			cbuf.put(today).flip();
			encoder.encode(cbuf, buf, true);
			buf.flip();
			System.out.println(buf.toString());

			MessageInfo messageInfo = MessageInfo.createOutgoing(null,0);
			sc.send(buf, messageInfo);


			cbuf.clear();
			buf.clear();

			sc.close();
		}
	}
	

	public static void printInfo(SctpChannel sctpChannel)
	{
		try
		{
			System.out.println("sctpChannel.getRemoteAddresses() = " + sctpChannel.getRemoteAddresses());
			System.out.println("sctpChannel.getAllLocalAddresses() = " + sctpChannel.getAllLocalAddresses());
			System.out.println("sctpChannel.isConnectionPending() = " + sctpChannel.isConnectionPending());
			System.out.println("sctpChannel.isOpen() = " + sctpChannel.isOpen());
			System.out.println("sctpChannel.isRegistered() = " + sctpChannel.isRegistered());
			System.out.println("sctpChannel.provider() = " + sctpChannel.provider());
			System.out.println("sctpChannel.association() = " + sctpChannel.association());
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
	}

	/*
	private Socket createNewSocket (int port) {
		Socket newSock = new Socket();

		return newSock;
	}
	 */
	/*
	private void newSessionAlert() {
		EventHandler.create( ActionListener.class, hostTerminal, "doActionEvent", "");
	}
	 */
}
